$(document).ready(function () {

    $("tr button").click(function () {

        let jishu = 0;

        let id = $(this).parent().parent().children("td").eq(0).text();
        let name = $(this).parent().parent().children("td").eq(1).text();
        let dataRiqi = $(this).parent().parent().children("td").eq(7).text();
        let idcard = $(this).parent().parent().children("td").eq(6).text();
        let phone = $(this).parent().parent().children("td").eq(5).text();
        let yisheng = $(this).parent().parent().children("td").eq(8).text();
        let riqi = $(this).parent().parent().children("td").eq(7).text();
        let array = new Array();
        let arrayCount = new Array();

        //查看详情
        let str2 = ``;

        $.ajax({
            url: "http://localhost:8080/drugY/selDrug",
            type: "POST",
            data: {
                id: id,
                date: riqi
            },
            dataType: "json",
            success: function (data) {
                str2 += `<table style="margin-left: 40px">`;
                str2 += `<tr><td style="width: 5%">序号</td><td style="width: 10%">药品名称</td><td style="width: 5%">数量</td><td style="width: 10%"> 用法/用量</td></tr>`;
                for (let i = 0; i < data.length; i++) {
                    array[i] = data[i].drugName;
                    arrayCount[i] = data[i].count;
                    str2 += `<tr> 
                                <td >${i + 1}</td>
                                <td >${data[i].drugName}</td>
                                <td >${data[i].count}</td>
                                <td >${data[i].type}</td>
                              </tr>        
                    `;
                }
                str2 += `</table>`;
                let str = `<p>
              &nbsp;&nbsp;  患者卡号:<u style="color: #009f95">${id}</u>  &nbsp;&nbsp;&nbsp;  
          姓名：<u style="color: #009f95">${name}</u>    &nbsp;&nbsp;
          电话：<u style="color: #009f95">${phone}</u> 
          <br>
                        
                患者身份证号:<u style="color: #009f95">${idcard}</u>
                 &nbsp;&nbsp;开药日期:<u style="color: #009f95">${dataRiqi}</u>
            </p>
            <p>——————————————————————————</p>
            <p>
                ${str2}
            </p>
            <p>——————————————————————————</p>
            <p style="margin-left: 50px">开药负责人:彤机       &nbsp;&nbsp;         开药医生:<u style="color: #009f95">${yisheng}</u></p>`;
                // alert(str2+"测试");
                swal({
                    title: "华子医院处方笺",
                    text: str,
                    showCancelButton: true,
                    confirmButtonText: "打印药方单",
                    closeOnConfirm: false,
                    closeOnCancel: false,
                    cancelButtonText: "取消打印",
                    html: true     //true：表示写的文本是html元素
                }, function (falg) {
                    jishu++;
                    if (jishu > 1) {
                        alert("操作次数过多!请稍等！")
                    } else {
                        if (falg) {
                            let s = array.join("-");
                            let s1 = arrayCount.join("-");
                            $.ajax({
                                url: "http://localhost:8080/drugY/updStatus",
                                type: "POST",
                                data: {
                                    id: id,
                                    date: riqi,
                                    shu1: s,
                                    shu2: s1
                                },
                                dataType: "json",
                                success: function (data) {
                                    swal(data);
                                    if (data == "拿药成功!") {
                                        // window.print(str);
                                        location.reload();
                                    }
                                }
                            })
                        } else {
                            swal("您选择了取消!");
                        }
                    }
                });
            }
        })
    });
});






